package searchEngine;
import java.util.*;
import java.util.regex.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import AgentsDB.Agent;

public class SearchEngine {
	
	public static StringBuilder search(String searchExpr, String Url, String separator) throws MalformedURLException, IOException {
		URL u = new URL(Url);
		return search(searchExpr, u, separator);
	}	
	public static StringBuilder search(String searchExpr, URL Url, String separator ){	
		
		Pattern agentBlock = Pattern.compile(searchExpr,Pattern.DOTALL);
		StringBuilder searchResults = new StringBuilder();		
		StringBuilder urlSource;
		try {
			urlSource = utils.ReaderFactory.readUrlData(Url, "ISO-8859-1");
			Matcher agentsMatcher = agentBlock.matcher(urlSource);

			while (agentsMatcher.find()) {
				searchResults.append(agentsMatcher.group()+separator);
			}
		} catch (IOException e) {
			// Logging should be applied
			e.printStackTrace();
			searchResults.append("Exception "+e.getMessage());
		}

		
		return searchResults;
	}
	
	public static Map<URL,StringBuilder> search(String searchExpr, HashSet<URL> urlList, String separator) throws IOException {	
		Iterator<URL> iterator = urlList.iterator();
		Map<URL,StringBuilder> searchResults = new HashMap<URL,StringBuilder>();
			while(iterator.hasNext()){
				URL url = new URL(iterator.next().toString());
				StringBuilder urlSearchResult = search(searchExpr,url, separator); 
				searchResults.put(url,urlSearchResult);
			}
		return searchResults;
	}
    
	/**
     * Attempts to find agents names and phones on page of requested urlToScan.
     * agentMessageExpr used to decrease scopes of scanning.
     * For ex use <div>(.*?)</div> to search only in div tags.
     *
     */
	public static Set<Agent> searchAgents(URL urlToScan,String agentMessageExpr){
		String agentNameExpr = ".{0,15}((Ð°|Ð)Ð³ÐµÐ½Ñ).{0,15}";
		String agentPhoneExpr = "[2-6,9]{2}[), ,-]{0,2}\\d\\d\\d[ ,-]?\\d\\d[ ,-]?\\d\\d";
		Pattern agentNamePattern = Pattern.compile(agentNameExpr);
		Pattern agentPhonePattern = Pattern.compile(agentPhoneExpr);
		String separator = "---------------------------------";
		StringBuilder temp = SearchEngine.search(agentMessageExpr,urlToScan,separator);
		Scanner scanner = new Scanner(temp.toString()).useDelimiter(separator);
		Date date = new Date();
		HashSet<Agent> agents = new HashSet<Agent>();
		while (scanner.hasNext()) {
			String message = scanner.next();
			if (message.length() > 1) {
				Matcher agentNameMatcher = agentNamePattern.matcher(message);
				if (agentNameMatcher.find()) {
					String agentName = agentNameMatcher.group();
					Matcher agentPhoneMatcher = agentPhonePattern.matcher(message);
					while (agentPhoneMatcher.find()) {
						Long phoneNumber = new Long(agentPhoneMatcher.group()
								.replaceAll("[) ,-]", ""));
						Agent agent = new Agent(phoneNumber, agentName, date,urlToScan.getHost());
						agents.add(agent);
					}
				}
			}
		}
		return agents;
	}
	public static Set searchAgents(URL urlToScan)throws Exception{
		String agentMessageExpr = "<(p|div|span)>(.*?)</(p|div|span)>";
		return searchAgents(urlToScan, agentMessageExpr);
	}
}
